/**
 * @file aicl_dev_001.cpp
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <math.h>
#include <ctime>
#include <chrono>

#include "acl/acl.h"
#include "aicl.h"
#include "utils.h"

int32_t deviceId = 0;
int32_t deviceIdGet;
uint32_t count = 0;


int main()
{
    const char *workPath = getenv("OPENCANN_TPATH");
    if (workPath == NULL) {
        ERROR_LOG("OPENCANN_TPATH is not set");
        return OPENCANN_FAILED;
    }
    std::string configPath = workPath;
    configPath += "/common/test_data/config/init_config.json";

    // set config file
    if (aiclInit(configPath.c_str()) != AICL_RET_SUCCESS) {
        ERROR_LOG("aiclInit failed");
        return OPENCANN_FAILED;
    }
    // device
    if (aiclrtGetDeviceCount(&count) != AICL_RET_SUCCESS) {
        ERROR_LOG("getDeviceCount failed");
        return OPENCANN_FAILED;
    }
    if (count < 1) {
        ERROR_LOG("getDeviceCount failed");
        return OPENCANN_FAILED;
    } else {
        INFO_LOG("Get device count %d success", count);
    }
    aiclRet ret;
    ret = aiclrtSetDevice(deviceId); 
    if (ret != AICL_RET_SUCCESS) {
        ERROR_LOG("Set device[%d] failed.", deviceId);
        return OPENCANN_FAILED;
    } else {
        INFO_LOG("Set device[%d] success", deviceId);
    }
    ret = aiclrtGetDevice(&deviceIdGet);
    if (ret != AICL_RET_SUCCESS) {
        ERROR_LOG("Get device failed.");
        return OPENCANN_FAILED;
    }
    if (deviceIdGet != deviceId) {
        ERROR_LOG("Get device[%d] diff with set deveice[%d].", deviceIdGet, deviceId);
    } else {
        INFO_LOG("Get device success");
    }
    ret = aiclrtResetDevice(deviceId);
    if (ret != AICL_RET_SUCCESS) {
        ERROR_LOG("Reset device[%d] failed.", deviceId);
        return OPENCANN_FAILED;
    } else {
        INFO_LOG("Reset device[%d] success", deviceId);
    }

    // aiclFinalize
    if (aiclFinalize() != AICL_RET_SUCCESS) {
        ERROR_LOG("aiclFinalize failed");
        return OPENCANN_FAILED;
    }
    return OPENCANN_SUCCESS;
}
